package com.example.demo.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * @description:
 * @author: ZZZ
 * @time: 2023/9/12 18:20
 */
@Configuration
public class MQConfig {
    @Bean
    public DefaultMQProducer producer() throws MQClientException {
        //1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.指定Nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动producer
        producer.start();
        return producer;
    }

    @Autowired DefaultMQProducer producer;


    @PreDestroy
    public void MqDestroy() {
        System.out.println("====mqprodercer shutdown=====");
        producer.shutdown();
    }

}
